(function($){
	var get_prefix = function(id){
		$.getJSON("/get_prefix/", {'id': id}, function(data){
			var html = "<option value=''>---------</option>";
			$.each(data.pre, function(k, v){
				if($("#id_platform").val() == "A"){
					v1 = v.replace(/-/g, "_");
				}else{
					v1 = v;
				}
				if($("#id_bundle_id_prefix").val() == v1){
					html += "<option selected='selected' value='" + v1 + "'>" + v1 + "</option>";
				}else{
					html += "<option value='" + v1 + "'>" + v1 + "</option>";
				}
			})
			$("#bundle_id_prefix_select").html(html);
		});
	};

	var get_studios = function(id){
		$.getJSON("/get_studios/", {'store': $("#id_app_store").val()}, function(data){
			var html = "<option value=''>---------</option>";
			$.each(data.studios, function(k, v){
				if(v.id == id){
					html += "<option selected='selected' value='" + v.id + "'>" + v.name + "</option>";
				}else{
					html += "<option value='" + v.id + "'>" + v.name + "</option>";
				}
			})
			$("#id_studio").html(html);
		})
	};
	
	var get_format = function(id){
		$.getJSON("/get_format/", {'id': id, 'store': $("#app_store_radio input[name=app_store]:checked").val()}, function(data){
			$("#id_prj_code").val(data.res);
		})
	}

	$(document).ready(function(){
		$("#id_bundle_id_prefix").css('display', 'none');
		$("#id_bundle_id_prefix").after("\
	<select id='bundle_id_prefix_select' name='bundle_id_prefix'>\
	</select>")
		var selected_studio = $("#id_studio option:selected").val();
		get_studios(selected_studio);
		get_prefix(selected_studio);
		$("#id_studio").change(function(e){
			get_prefix(e.target.value);
			get_format(e.target.value);
		});
	});
})(django.jQuery);
